import java.sql.SQLRecoverableException;
import java.util.ArrayList;
import java.util.Scanner;


public class Player extends Player_comp{
    Scanner sc = new Scanner(System.in);

    public Player(String nick)
    {
        super(nick);
    }

    public int pickNumOfExCards()
    {
        return numOfExCards;
    }


    public ArrayList<Card> pickCardsToChange()
    {

        ArrayList<Card> toChange = new ArrayList<>();
        String chosing;
        do{
            toChange.clear();

            System.out.print("Ktore karty chcesz wymienic?: ");
            chosing = sc.next();
            boolean Error = false;
            if(chosing.contains(",") || chosing.length()==1)
            {
                String[] indexOfCard = chosing.trim().split(",");
                indexOfCard = delHollow(indexOfCard);

                if(indexOfCard.length <= 4)
                {
                    for(String indexString : indexOfCard)
                    {
                        if(within15(indexString))
                        {
                            int indexCard = Integer.parseInt(indexString);
                            toChange.add(hand.pickCard(indexCard - 1));
                        }
                        else
                        {
                            System.out.println("Wybrana karta nie istnieje.");
                        }
                    }
                }
                else
                {
                    System.out.println("Mozesz wymienic maksymalnie 4-ry Karty.");
                    Error = true;
                }
            }
            else
            {
                System.out.println("Bledny format danych.");
                Error = true;
            }
            if(Error)
            {
                System.out.println("Sproboj ponownie.");
                chosing = "N";
            }
            else {
                System.out.println("Wybrales: " + Card.downCardList(toChange));
                System.out.print("Zatwierdzasz wybor?: ");
                chosing = sc.next();
            }
        }while (!chosing.equals("T"));
        numOfExCards = toChange.size();
        return toChange;
    }

    private boolean within15(String str)
    {
        boolean iffrombox = false;
        char[] charsInString = str.toCharArray();
        if(charsInString.length == 1)
        {
            char cha = charsInString[0];
            iffrombox = Character.isDigit(cha) && Character.getNumericValue(cha)>0 &&  Character.getNumericValue(cha)<6;
        }
        return iffrombox;
    }

    private String[] delHollow(String[] tab)
    {
        ArrayList<String> dynamicTabWithOutHollow = new ArrayList<>();
        for(String str : tab)
        {
            if(!str.isEmpty())
            	dynamicTabWithOutHollow.add(str);
        }
        String[] tabWithOutHollow = new String[dynamicTabWithOutHollow.size()];
        tabWithOutHollow = dynamicTabWithOutHollow.toArray(tabWithOutHollow);

        return tabWithOutHollow;
    }

    public String getName() {

        return nick;
    }


}
